<?php

MYTCONN();
$CurLabel = $_SESSION[$SESSST . 'CURS'];

//Functions
include($GLOBALS['OBJ'] . 'obj.mathgrids.mex');
//Vars
$TotalsTitle = '<tr><td colspan="12">Totais</td></tr>
        <tr bgcolor="#000000"><td colspan="12" height="2"></td></tr>';

$NoResults = '<tr bgcolor="#F2F2F2"><td colspan="12">Não existem resultados para apresenta??o.</td></tr>';

$TotalEuros = 0;
$TotalReais = 0;
$TotalCargo = 0;
$TotalTax = 0;
$Totremessas = 0;

## Set Fields
$SentAmount = 'IF(FraModo=1,FraEnviado+FraProm,FraEnviado)';
$PaidAmount = 'IF(FraModo=1,FraARecibir,FraARecibir)';
$FeesAmount = 'IF(FraModo=1,FraCargo,FraIncComisionCo)';

if ($_POST['Ordering2'] == 'DateDay') {
    $_POST['Ordering2'] = 'DATE(' . $_POST['TimeType'] . ')';
}
if ($_POST['OrderBy'] == 'DateDay') {
    $_POST['OrderBy'] = 'DATE(' . $_POST['TimeType'] . ')';
}

if (isset($_POST['Op_Corr'])) {

    $Bancos = " AND Fracorresponsal IN (";

    $Bank = $_POST['Op_Corr'];
    $B = count($Bank);
    for ($b = 0; $b < $B; $b++) {
        if ($Bank[$b] == 307) {
            $ModelType = 1;
        }
        if ($b == 0) {
            $Bancos .= "{$Bank[$b]}";
        } else {
            $Bancos .= ",{$Bank[$b]}";
        }
    }
    $Bancos .= ") ";
} else {
    $Bancos = '';
}
$Lojas = '';
if (isset($_POST['Offices'])) {
    $Lojas = " AND FraOrigen0 IN (";

    $Office = $_POST['Offices'];
    $O = count($Office);
    for ($c = 0; $c < $O; $c++) {
        if ($c == 0) {
            $Lojas .= "{$Office[$c]}";
        } else {
            $Lojas .= ",{$Office[$c]}";
        }
    }
    $Lojas .= ") ";
}
$Country = '';
if (isset($_POST['BnfPais'])) {
    $Country = " AND (BnfPais IN (".implode($_POST['BnfPais']).") OR FraBnfPais IN (".implode($_POST['BnfPais'])."))";
}

if (isset($_POST['join'])) {
    $OrderReport = "FraOrigen0,FraReferenciaAg," . $_POST['TimeType'];
} elseif ($_POST['Ordering2'] == 'FraTasa') {
    $OrderReport = "FraCambioRl";
} else {
    $OrderReport = "FraOrigen0";
}

if ($_POST['Order'] != "") {
    $Order = split("-", $_POST['Order']);
    $Type = "FraOrigen0 = '{$Order[0]}' AND FraReferenciaAg = '{$Order[1]}'";
} else {
    $Type = $_POST['TimeType'] . " BETWEEN '{$_POST['TimeStart']} 00:00:00' AND '{$_POST['TimeEnd']} 23:59:59'";
}

if (isset($ModelType) && $ModelType == 1) {
    $Colums = '<td NOWRAP>Comissão Money</td>
        <td NOWRAP>ComissÃo Correspondente</td>
	<td NOWRAP>Imposto Selo</td>';
} else {
    $Colums = '<td NOWRAP>Cambio</td>
	<td NOWRAP>Taxa</td>';
}

$prStr = '<table width="100%">
        <tr style="font-weight:bold;height:20px;text-align:center">
          <td NOWRAP>' . $_POST['Ordering2'] . '</td>
          <td NOWRAP>Recebido </td>
          <td NOWRAP>Entregue</td>
          ' . $Colums . '
          <td NOWRAP>Remessas</td>
          <td NOWRAP>Banco</td>
        </tr>';

$GET = $GLOBALS['MYT']->db_array("SELECT
     SUM(IF(FraStatus=10,-1,1)) AS COUNTER,
     {$_POST['Ordering2']} AS RowLabel,
     SUM($SentAmount) AS FraEnviado,
     SUM(FraCargo) AS Cargo,
     SUM(FraComisionAg) AS FraComisionAg,
     SUM(FraComisionCorr) AS FraComisionCorr,
     SUM(FraARecibir) AS FraARecibir,
     SUM(FraARecibir/($SentAmount)) AS Rate,
     FraMonDPago,FraMonCobro,
     '' AS Beneficiario,
     CorrNombre AS Corresponsal
    FROM
    facturas
	LEFT JOIN facturasextjoins ON FraID = FraExtID
    LEFT JOIN beneficiarios ON BnfNumero = FraBnfNumero
    INNER JOIN hd_Paises ON BnfPais = pai_CodElite
    INNER JOIN origenes ON FraOrigen0 = OriNumero
    INNER JOIN corresponsales ON FraCorresponsal = CorrNumero
    LEFT OUTER JOIN ordenespago ON Op_Orden = FraOrdenPago
    WHERE $Type AND FraStatus IN ({$_POST['status']}) $Bancos $Lojas $Country
    GROUP BY {$_POST['Ordering2']},FraMonDPago,FraMonCobro
    ORDER BY {$_POST['Ordering2']} ASC");

$Totremessas = $GET[0] + 1;
if ($Totremessas == 1) {
    $prStr .= $NoResults;
} else {
    for ($i = 1; $i <= $Totremessas; $i++) {
        if ($i == $Totremessas) {
            $prStr .= $TotalsTitle;
            $inCur = '';
            $outCur = '';
        }else{
            $inCur = $CurLabel[FieldResult($GET, $i, 'FraMonCobro')]['CurLabel'];
            $outCur = $CurLabel[FieldResult($GET, $i, 'FraMonDPago')]['CurLabel'];
        }
        $prStr .= '<tr style="padding:3px;background-color:' . gridlines($i) . '" ';
        if (number_format(FieldResult($GET, $i, 'FraEnviado'), 2) < 0) {
            $prStr .= 'style="color:#FF0000"';
        }
        $prStr .= '>
	<td NOWRAP>' . FieldResult($GET, $i, 'RowLabel') . '</td>
	<td NOWRAP style="padding:3px;text-align:right">' . number_format(FieldResult($GET, $i, 'FraEnviado'), 2) . $inCur . '</td>
	<td NOWRAP style="padding:3px;text-align:right">' . number_format(FieldResult($GET, $i, 'FraARecibir'), 2) . $outCur . '</td>';
        if (isset($ModelType) && $ModelType == 1) {
            $prStr .= '<td NOWRAP style="padding:3px;text-align:right">' . number_format(((FieldResult($GET, $i, 'FraComisionCorr') / 3) * 1), 2) . $inCur . '</td>
	<td NOWRAP style="padding:3px;text-align:right">' . number_format(((FieldResult($GET, $i, 'FraComisionCorr') / 3) * 2), 2) . $inCur . '</td>
	<td NOWRAP style="padding:3px;text-align:right">' . number_format(FieldResult($GET, $i, 'FraComisionAg'), 2) . $inCur . '</td>';
        } else {
            $prStr .= '<td NOWRAP style="padding:3px;text-align:right">' . number_format(FieldResult($GET, $i, 'Rate') / FieldResult($GET, $i, 'COUNTER'), 4) . '</td>';
            $prStr .= '<td NOWRAP style="padding:3px;text-align:right">' . number_format(FieldResult($GET, $i, 'Cargo'), 2) . $inCur . '</td>';
        }
        $prStr .= '<td NOWRAP style="padding:3px;text-align:right">' . FieldResult($GET, $i, 'COUNTER') . '</td>
        <td NOWRAP>' . FieldResult($GET, $i, 'Corresponsal') . '</td>
		</tr>';
    }
}
$prStr .= '</table>';
echo $prStr;